<?php

class imagem
{

    static function upload($nome_img,$dir,$id,$tumb=NULL,$width = 200, $height = 200)
    {
        $img = $_FILES[$nome_img];
        $imageAdapter = new Zend_File_Transfer_Adapter_Http();
        $imageAdapter->setDestination($dir);
        $filenew = '';
        $ext = '';
        if(is_uploaded_file($img['tmp_name'])){

            foreach (glob($dir.$id.'.*') as $filename) unlink($filename);

            $ext = end(explode('.', $img['name']));

            $filenew = $dir.$id.'.'.$ext;

            $imageAdapter->addFilter('Rename', array(
                    'target' => $filenew,
                    'overwrite' => true
            ));

            if (!$imageAdapter->receive($nome_img)){ //A Imagem Não Foi Recebida Corretamente
                $messages = $imageAdapter->getMessages[$nome_img];
                
            }else{ //Arquivo Enviado Com Sucesso
                $filename = $imageAdapter->getFileName($nome_img);
                chmod($filename, 0777);
                
                if($tumb == TRUE){
                    
                    foreach (glob($dir.$id.'_tb.*') as $filenewp) unlink($filenewp);
                    
                    Zend_Loader::loadClass('WideImage','library/wideimage/lib');
                    
                    $image = WideImage::load($filename);
                    
                    //$resized = $image->resize($width, $height, 'inside'); 
                    //$resized = $image->crop('0', '0' , $width, $height);
                    $resized = $image->resize($width, $height, 'outside')->crop('50% - '.($width/2), '50% - '.($height/2), $width, $height);
        
                    
                    $filenewp = $dir.$id.'_tb.'.end(explode('.', $filename));
                    
                    $resized->saveToFile($filenewp);
                    chmod($filenewp, 0777);
                }
            }
        }
        return array('filenew'=>$filenew,'ext'=>$ext);
    }
    
    static function rename($id,$dir,$file,$tumb=NULL)
    {
        $filenew = $dir.$id.'.'.end(explode('.', $file));
        rename($file,$filenew);
        if($tumb == TRUE){
            $filenewp = $dir.$id.'_tb.'.end(explode('.', $file));
            $file = str_replace('.', '_tb.', $file);
            rename($file,$filenewp);
        }
        
        /*    
        $img = $dir.$id.'.*';
        foreach (glob($img) as $src) {
            $filenew = $dir.$id.'.'.end(explode('.', $src));
            rename($src,$filenew);
        }
         * *
         */
    }
    static function delete($id,$dir,$tumb=NULL)
    {
        $arr = glob($dir.$id.'.*');
        if(isset($arr[0]))
        foreach($arr as $file) {
            unlink($file);
            if($tumb == TRUE && file_exists(str_replace('.', '_tb.', $file))) 
                unlink(str_replace('.', '_tb.', $file));
        }
    }
    
    static function formatNomeArquivo($id,$datahora) {
        $str =  md5($id.$datahora);
        return $str;
    }
    
    static function reize($filename, $filenewp, $width = 200, $height = 200)
    {
           
        Zend_Loader::loadClass('WideImage','library/wideimage/lib');

        $image = WideImage::load($filename);
        
        //$resized = $image->resize($width, $height, 'inside'); 
        //$resized = $image->crop('50% - '.($width/2), '50% - '.($height/2) , $width, $height);
        
        $resized = $image->resize($width, $height,'outside')->crop('50% - '.($width/2), '50% - '.($height/2), $width, $height);
                    
        $resized->saveToFile($filenewp);
        
        return true;
    }
    
    static function reizeFull($filename, $filenewp, $width = 200, $height = 200, $fit = 'outside')
    {
           
        Zend_Loader::loadClass('WideImage','library/wideimage/lib');

        $image = WideImage::load($filename);

        //$resized = $image->resize($width, $height, $fit);
        
        $resized = $image->resize($width, $height, $fit)->crop('50% - '.($width/2), '50% - '.($height/2), $width, $height);
        
        $resized->saveToFile($filenewp);
        
    }
    
    static function insertGaleria($data)
    {
        error_reporting(E_ALL|E_STRICT);
        ini_set('display_errors', true);
        //date_default_timezone_set('Europe/London');
        $rootDir = dirname(dirname(__FILE__));
        //echo dirname(__FILE__); C:\wamp\www\zf-tutorial\testdb
        //echo $rootDir;          C:\wamp\www\zf-tutorial
        set_include_path( $rootDir . '/library'. PATH_SEPARATOR . get_include_path());

        require_once('Zend/Loader.php');
        Zend_Loader::loadClass('Zend_Db');
        //Zend_Loader::loadClass('Zend_Db_Table_Abstract');
        Zend_Loader::loadClass('Zend_Db_Table');
        //Zend_Controller_Front::run('/path/to/controllers');
        //Zend::loadClass('Zend_Db');
        
        //$db = Zend_Db_Table::getDefaultAdapter();
        
        Zend_Loader::loadClass('Zend_Config_Ini');        
        $config = new Zend_Config_Ini($rootDir.'/application/configs/application.ini');
        $arr = $config->toArray();
        
        Zend_Loader::loadClass('Zend_Db_Adapter_Pdo_Mysql'); 
        $db = new Zend_Db_Adapter_Pdo_Mysql(array(
                    'host'     => $arr['production']['resources']['db']['params']['host'],
                    'username' => $arr['production']['resources']['db']['params']['username'],
                    'password' => $arr['production']['resources']['db']['params']['password'],
                    'dbname'   => $arr['production']['resources']['db']['params']['dbname'],
                ));

        $sql = 'insert into GALERIA (GAL_IDIMG,GAL_NOM,GAL_ORD,ALU_ID) values("'.$data['GAL_IDIMG'].'","'.$data['GAL_NOM'].'","'.$data['GAL_ORD'].'","'.$data['ALU_ID'].'")';
        $statement = $db->query($sql);
        
    }

}


